<template>

  <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="100px">
    <div class="com-form-title">基础信息</div>
    <el-row>
      <el-col :span="12">
        <el-row>
          <el-col :span="12">
            <el-form-item label="分包结算编号" prop="settleNumber">
              <el-input v-model="form.settleNumber" :disabled="disableForm" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分包队伍编号" prop="teamNumber">
              <el-input v-model="form.teamNumber" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="分包队伍名称" prop="teamName">
              <el-input v-model="form.teamName" :disabled="disableForm" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分包合同编号" prop="contractNumber">
              <el-input v-model="form.contractNumber" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-col>
      <el-col :span="12">
        <el-row>
          <el-col :span="12">
            <el-form-item label="分包合同名称" prop="contractName">
              <el-input v-model="form.contractName" :disabled="disableForm" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同额" prop="contractAmount">
              <el-input v-model="form.contractAmount" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="结算期次(年+月)" prop="period">
              <el-input v-model="form.period" :disabled="disableForm" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="本次结算金额" prop="thisSettleAmount">
              <el-input v-model="form.thisSettleAmount" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-col>
      <el-col :span="12">
        <el-row>
          <el-col :span="12">
            <el-form-item label="开累结算金额" prop="accruedSettleAmount">
              <el-input v-model="form.accruedSettleAmount" :disabled="disableForm" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="form.projectName" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="流程状态" prop="status">
              <el-select v-model="form.status" :disabled="disableForm" style="width: 100%;" placeholder="请选择流程状态">
                <el-option v-for="item in workflowStatus" :key="item.name" :label="item.name" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-col>
      <el-col :span="12">
        <el-row>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" :disabled="disableForm" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编制人" prop="founderName">
              <el-input v-model="form.founderName" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="编制人单位" prop="founderUnit">
              <el-input v-model="form.founderUnit" :disabled="disableForm" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编制时间" prop="createTime">
              <el-date-picker v-model="form.createTime" style="width:100%" type="date" :disabled="disableForm" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-col>
    </el-row>

    <div class="btns" style="padding-bottom: 10px;">
      <el-button type="primary" @click="showAddDetailModal = true;">新增行</el-button>
      <el-button type="primary" @click="delDetailDataItem">删除行</el-button>
    </div>
    <com-table
      v-model="selectRow"
      :no-page="true"
      :columns="columns"
      highlight-current-row
      :data="detailDataList"
    >
      <template v-slot:conNumber="{row}">
        <el-input v-model="row.conNumber" />
      </template>
      <template v-slot:conName="{row}">
        <el-input v-model="row.conName" />
      </template>
      <template v-slot:conUnit="{row}">
        <el-input v-model="row.conUnit" />
      </template>
      <template v-slot:conUnitPrice="{row}">
        <el-input v-model="row.conUnitPrice" />
      </template>
      <template v-slot:conSumPrice="{row}">
        <el-input v-model="row.conSumPrice" />
      </template>
      <template v-slot:conCount="{row}">
        <el-input v-model="row.conCount" />
      </template>
      <template v-slot:tsCount="{row}">
        <el-input v-model="row.tsCount" />
      </template>
      <template v-slot:tsAmount="{row}">
        <el-input v-model="row.tsAmount" />
      </template>
      <template v-slot:asCount="{row}">
        <el-input v-model="row.asCount" />
      </template>
      <template v-slot:asAmount="{row}">
        <el-input v-model="row.asAmount" />
      </template>
      <template v-slot:ctsCount="{row}">
        <el-input v-model="row.ctsCount" />
      </template>
      <template v-slot:ctsAmount="{row}">
        <el-input v-model="row.ctsAmount" />
      </template>
      <template v-slot:casCount="{row}">
        <el-input v-model="row.casCount" />
      </template>
      <template v-slot:casAmount="{row}">
        <el-input v-model="row.casAmount" />
      </template>
      <template v-slot:remark="{row}">
        <el-input v-model="row.remark" />
      </template>
    </com-table>
    <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
    <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />

  </el-form>
</template>

<script>
import { addTransferContractSettleInfo, updateTransferContractSettleInfo, getTransferContractSettleInfoDetail } from '@/api/contractManage'
// import { getAreaList } from '@/api/common'
import { workflowStatus } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'
export default {
  name: 'AddTransferContractSettleInfo',
  components: {
    fileUploader
  },
  props: {

    optType: {
      type: String,
      default: 'detail'
    },

    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      showFileModal: false,
      targetList: [],
      workflowStatus,

      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        settleNumber: '',
        teamNumber: '',
        teamName: '',
        contractNumber: '',
        contractName: '',
        contractAmount: '',
        period: '',
        thisSettleAmount: '',
        accruedSettleAmount: '',
        projectName: '',
        addWorkflowId: '',
        status: '',
        remark: '',
        founderName: '',
        founderUnit: '',
        createTime: ''
      },
      showAddDetailModal: false,
      selectRow: null,
      detailDataList: [],
      detailDataItem: {
      },
      columns: [
        {
          label: '合同-清单编号',
          slot: 'conNumber'
        },
        {
          label: '合同清单名称',
          slot: 'conName'
        },
        {
          label: '合同-单位',
          slot: 'conUnit'
        },
        {
          label: '合同-单价(元)',
          slot: 'conUnitPrice'
        },
        {
          label: '合同-合价(元)',
          slot: 'conSumPrice'
        },
        {
          label: '合同-数量',
          slot: 'conCount'
        },
        {
          label: '本次结算-结算数量',
          slot: 'tsCount'
        },
        {
          label: '本次结算-结算金额',
          slot: 'tsAmount'
        },
        {
          label: '开累结算-结算数量',
          slot: 'asCount'
        },
        {
          label: '开累结算-结算金额',
          slot: 'asAmount'
        },
        {
          label: '按合同本次结算-结算数量',
          slot: 'ctsCount'
        },
        {
          label: '按合同本次结算-结算金额',
          slot: 'ctsAmount'
        },
        {
          label: '按合同开累结算-结算数量',
          slot: 'casCount'
        },
        {
          label: '按合同开累结算-结算金额',
          slot: 'casAmount'
        },
        {
          label: '备注',
          slot: 'remark'
        }
      ],
      showTargetSetListModal: false,
      fileList: [
      ],
      rules: {
        name: [
          { required: true, message: '单位名称', trigger: 'change' }
        ],
        unifiedSocialCreditCode: [
          { required: true, message: '统一社会信用代码', trigger: 'change' }
        ]
      },
      detailRules: {
        conNumber: [
          { required: true, message: '请输入合同-清单编号', trigger: 'change' }
        ],
        conName: [
          { required: true, message: '请输入合同清单名称', trigger: 'change' }
        ],
        conUnit: [
          { required: true, message: '请输入合同-单位', trigger: 'change' }
        ],
        conUnitPrice: [
          { required: true, message: '请输入合同-单价(元)', trigger: 'change' }
        ],
        conSumPrice: [
          { required: true, message: '请输入合同-合价(元)', trigger: 'change' }
        ],
        conCount: [
          { required: true, message: '请输入合同-数量', trigger: 'change' }
        ],
        tsCount: [
          { required: true, message: '请输入本次结算-结算数量', trigger: 'change' }
        ],
        tsAmount: [
          { required: true, message: '请输入本次结算-结算金额', trigger: 'change' }
        ],
        asCount: [
          { required: true, message: '请输入开累结算-结算数量', trigger: 'change' }
        ],
        asAmount: [
          { required: true, message: '请输入开累结算-结算金额', trigger: 'change' }
        ],
        ctsCount: [
          { required: true, message: '请输入按合同本次结算-结算数量', trigger: 'change' }
        ],
        ctsAmount: [
          { required: true, message: '请输入按合同本次结算-结算金额', trigger: 'change' }
        ],
        casCount: [
          { required: true, message: '请输入按合同开累结算-结算数量', trigger: 'change' }
        ],
        casAmount: [
          { required: true, message: '请输入按合同开累结算-结算金额', trigger: 'change' }
        ],
        remark: [
          { required: true, message: '请输入备注', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    }
  },
  watch: {
    data: {
      handler(newData, oldData) {
        if (newData.id) {
          this.getDocDetail(newData.businessId)
        }
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {

  },
  methods: {
    addDetail() {
      this.$refs.detailForm.validate((valid) => {
        if (valid) {
          const item = JSON.parse(JSON.stringify(this.detailDataItem))
          this.detailDataList.push(item)
          this.showAddDetailModal = false
          this.$refs.detailForm.resetFields()
        }
      })
    },
    cancelAddDetail() {
      this.showAddDetailModal = false
    },
    delDetailDataItem() {
      if (this.selectRow) {
        const index = this.detailDataList.indexOf(this.selectRow)
        this.detailDataList.splice(index, 1)
      }
    },
    getDocDetail(id) {
      getTransferContractSettleInfoDetail({ transferContractSettleInfoId: id }).then(res => {
        const { lstAttachment, transferContractSettleInfo } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
        Object.keys(this.form).forEach(key => {
          const source = transferContractSettleInfo[key]
          if (typeof source === 'boolean') {
            this.form[key] = source ? 1 : 0
          } else {
            this.form[key] = source
          }
        })
      })
    },
    deleteDetail() {
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = this.optType === 'add' ? {
            transferContractSettleInfo: {
              ...this.form
            },
            lstAttachment: this.fileList
          } : {
            ...this.form,
            id: this.data.id
          }
          const fn = this.optType === 'add' ? addTransferContractSettleInfo : updateTransferContractSettleInfo
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
::v-deep .el-form-item__label{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;

}
</style>
